package com.jiyun.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.jiyun.Result.Result;
import com.jiyun.common.pojo.home.User;
import com.jiyun.common.pojo.user.Member;
import com.jiyun.common.pojo.user.PlusRule;
import com.jiyun.user.mapper.MemberMapper;
import com.jiyun.user.mapper.PlusRuleMapper;
import com.jiyun.user.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private PlusRuleMapper plusRuleMapper;
    @Override
    public Result findMember(String token) {
        //判断是否为空
        if (token.isEmpty()){
            //返回失败
            return Result.fail();
        }
        //获取redis中的数据
        Object o = redisTemplate.opsForValue().get(token);
        //判断是否为空
        if (o!=null){
            //将json字符串转换为user对象
            User user = JSONUtil.toBean(o.toString(), User.class);
            //查询用户会员信息
            Member member= memberMapper.findMember(user);
            //查询会员对应等级的加阶规则
            PlusRule plusRule= plusRuleMapper.findPlusRule(member.getGrade()+1);
            //将加阶规则添加到会员对象中
            member.setPlusRule(plusRule);
            return Result.ok(member);
        }
        return Result.fail();
    }
}
